package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 用户登录功能
 * 要求：程序启动后要求用户在控制台上输入用户名密码
 * 使用该信息与userinfo表中记录进行匹配，若果匹配成功则登录成功，否则登录失败
 */
public class LoginDemo {
    public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);
//        System.out.println("用户登录,请输入用户名");
//        String userName = sc.next();
//        System.out.println("请输入密码");
//        String password = sc.next();
        LoginUserInfo userInfo = InputUtil.getInputObject(new LoginUserInfo(),"请输入用户名和密码");
        try (Connection conn = DBUtil.getConnection();){
            Statement state = conn.createStatement();
//          String sql = "SELECT id,username,password,nickname,age FROM userinfo WHERE username='"+userName+"'AND password='"+password+"' ";
            String sql = "SELECT id,username,password,nickname,age FROM userinfo WHERE username='"+userInfo.getUserName()+"'AND password='"+userInfo.getPassword()+"' ";
            System.out.println(sql);
            ResultSet rs = state.executeQuery(sql);
            if (rs.next()){
                    System.out.println("登录成功");
                }else{
                    System.out.println("登录失败");
                }
            }
         catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
